﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using iBookstore.Models;
using iBookstore.ViewModels;

namespace iBookstore.Controllers
{
    public class CartController : Controller
    {
        iBookstoreEntities storeDB = new iBookstoreEntities();

        //
        // GET: /Cart/
        public ActionResult Index()
        {
            var cart = ShoppingCart.GetCart(this.HttpContext);
            string notInStock = NotInStock(cart);
            if (notInStock != null)
            {
                ViewBag.Message = NotInStock(cart);
            }
            
 
            // Set up our ViewModel
            var viewModel = new CartViewModel
            {
                CartItems = cart.GetCartItems(),
                CartTotal = cart.GetTotal()
            };

            // Return the view
            return View(viewModel);
        }

        private String NotInStock(ShoppingCart shoppingCart)
        {
            List<Cart> carts = shoppingCart.GetCartItems();
            string response = "";

            foreach (Cart cart in carts)
            {
                if (storeDB.Books.Find(cart.BookId).Quantity - cart.Count < 0)
                {
                    response += cart.Book.Title + " - is not in stock. ";
                }
            }

            if (response == "")
            {
                return null;
            }
            else
            {
                return response;
            }
            
        }

        //
        // GET: /Store/AddToCart/5
        public ActionResult AddToCart(int id)
        {
            // Retrieve the album from the database
            var addedAlbum = storeDB.Books
                .Single(album => album.BookId == id);
 
            // Add it to the shopping cart
            var cart = ShoppingCart.GetCart(this.HttpContext);
 
            cart.AddToCart(addedAlbum);
 
            // Go back to the main store page for more shopping
            return RedirectToAction("Index");
        }

        //
        // AJAX: /Cart/RemoveFromCart/5
        [HttpPost]
        public ActionResult RemoveFromCart(int id)
        {
            // Remove the item from the cart
            var cart = ShoppingCart.GetCart(this.HttpContext);
 
            // Get the name of the album to display confirmation
            string albumName = storeDB.Carts
                .Single(item => item.RecordId == id).Book.Title;
 
            // Remove from cart
            int itemCount = cart.RemoveFromCart(id);
 
            // Display the confirmation message
            var results = new CartRemoveViewModel
            {
                Message = Server.HtmlEncode(albumName) +
                    " has been removed from your shopping cart.",
                CartTotal = cart.GetTotal(),
                CartCount = cart.GetCount(),
                ItemCount = itemCount,
                DeleteId = id
            };

            return Json(results);
        }

        //
        // GET: /Cart/CartSummary
        [ChildActionOnly]
        public ActionResult CartSummary()
        {
            var cart = ShoppingCart.GetCart(this.HttpContext);

            ViewData["CartCount"] = cart.GetCount();
            return PartialView("CartSummary");
        }
    }
}